cmake_minimum_required(VERSION 3.13)

set(APP PocketSurvival)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

project(${APP} LANGUAGES CXX C)

add_definitions(-DAPP_VERSION="0.0.1")

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# set(CMAKE_SKIP_RPATH TRUE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)   

set(pwd ${CMAKE_CURRENT_SOURCE_DIR})

set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)

# set(CMAKE_BUILD_TYPE "Debug")

IF(NOT CMAKE_BUILD_TYPE)
    SET(CMAKE_BUILD_TYPE "Release")
ENDIF()

add_definitions(-DNDEBUG)


 # 设定动态链接库的存储路径
IF(MSVC)
  set(LIB_OUT_PATH ${pwd}/build/bin)
ELSE()
  set(LIB_OUT_PATH ${pwd}/build/bin/${CMAKE_BUILD_TYPE})
ENDIF()

# MESSAGE("${PROJECT_NAME} 库将生成在 ${LIB_OUT_PATH}")
SET(LIBRARY_OUTPUT_PATH  ${LIB_OUT_PATH})


if(CMAKE_COMPILER_IS_GNUCXX)
  # 如果使用的编译器是 GCC-8 则需要使用到以下代码，链接c++17的文件系统库才能编译
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lstdc++fs -pthread -fPIC")
elseif(MSVC)
  # 设置以utf-8文件格式进行编译
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8")
  # 开启多核编译
  cmake_host_system_information(RESULT CPU_NUMBER_OF_LOGICAL_CORES QUERY NUMBER_OF_LOGICAL_CORES)
  add_compile_options($<$<CXX_COMPILER_ID:MSVC>:/MP${CPU_NUMBER_OF_LOGICAL_CORES}>)
endif()



# IF(MSVC)
#   set(OpenSSL_ROOT ${pwd}"/3rd/MSVC/openssl")
#   set(OpenSSL_INCLUDE ${OpenSSL_ROOT}/include)
#   set(OpenSSL_LIBRARIES ${OpenSSL_ROOT}/lib)

#   find_package(OpenSSL REQUIRED)

# ELSEIF(MINGW)
#   set(OpenSSL_ROOT ${pwd}"/3rd/MinGW/openssl")
#   set(OpenSSL_INCLUDE ${OpenSSL_ROOT}/include)
#   set(OpenSSL_LIBRARIES ${OpenSSL_ROOT}/lib)

#   find_package(OpenSSL REQUIRED)

# ELSEIF(UNIX AND NOT APPLE)
#   message("当前Linux系统")

#   if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64)")
#     message("cpu: arm64")
#     # set(OpenSSL_ROOT ${pwd}/3rd/Linux/arm64/openssl-3.0.7-glibc2.28)
#     # set(OpenSSL_INCLUDE ${OpenSSL_ROOT}/include)
#     # set(OpenSSL_LIBRARIES ${OpenSSL_ROOT}/lib)

#     # set(OPENSSL_ROOT_DIR ${pwd}/3rd/Linux/arm64/openssl-3.0.7-glibc2.28)
#     # set(OPENSSL_INCLUDE_DIR ${OPENSSL_ROOT_DIR}/include)
#     # set(OPENSSL_CRYPTO_LIBRARY ${OPENSSL_ROOT_DIR}/lib/libcrypto.so.3)
#     find_package(OpenSSL)

#   elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64)")
#     message("cpu: x86_64")
#     # set(OpenSSL_ROOT ${pwd}/3rd/Linux/x86_64/openssl-3.0.7)
#     # set(OpenSSL_INCLUDE ${OpenSSL_ROOT}/include)
#     # set(OpenSSL_LIBRARIES ${OpenSSL_ROOT}/lib)

#     # set(OPENSSL_ROOT_DIR ${pwd}/3rd/Linux/x86_64/openssl-3.0.7)
#     # set(OPENSSL_INCLUDE_DIR ${OPENSSL_ROOT_DIR}/include)
#     # set(OPENSSL_CRYPTO_LIBRARY ${OPENSSL_ROOT_DIR}/lib/libcrypto.so.3)

#     find_package(OpenSSL)

#   endif()

# ELSE()
#   MESSAGE(FATAL_ERROR "暂时不支持该系统")

# ENDIF()



include("CMake/AddDependencies.cmake")

build_dependencies()
include_directories(${zlib_BINARY_DIR})

add_library(${APP} SHARED)

add_subdirectory(src)
add_subdirectory(plugins)


link_dependencies(${APP})

if(CMAKE_COMPILER_IS_GNUCXX)

  # find_library(STD_CPP_FS stdc++fs)
  # GCC8 GCC7需要手动链接 filesystem 库
  if (CMAKE_HOST_UNIX)
    target_link_libraries(${APP} PRIVATE stdc++fs dl)
  else()
    target_link_libraries(${APP} PRIVATE stdc++fs)
  endif()
  # 检查GNU编译是否支持 -Wa,-mbig-obj 
  include(CheckCXXCompilerFlag)
  check_cxx_compiler_flag("-Wa,-mbig-obj" GNU_BIG_OBJ_FLAG_ENABLE)
  message(STATUS GNU_BIG_OBJ_FLAG_ENABLE=${GNU_BIG_OBJ_FLAG_ENABLE})
endif()

# 设置编译选项，突破单个文件 64k 限制
target_compile_options(${APP}
	PRIVATE 
	$<$<CXX_COMPILER_ID:MSVC>:/bigobj> 
	$<$<AND:$<CXX_COMPILER_ID:GNU>,$<BOOL:${GNU_BIG_OBJ_FLAG_ENABLE}>>:-Wa,-mbig-obj>) 

set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)

set_target_properties(${APP} PROPERTIES INSTALL_RPATH "$ORIGIN")

target_include_directories(${APP} PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>" "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include/PocketSurvival>" "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
include_directories(include/PocketSurvival)




# 下面是生成控制台程序

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# set(CMAKE_SKIP_RPATH TRUE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) 

SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY  ${LIB_OUT_PATH})

add_executable(PSTerminal main.cpp)

if (WIN32)
  target_link_libraries(PSTerminal PUBLIC PocketSurvival spdlog dbghelp)
else()
  target_link_libraries(PSTerminal PUBLIC PocketSurvival spdlog)
endif()

set_target_properties(PSTerminal PROPERTIES INSTALL_RPATH "$ORIGIN")
